package com.qzxl.conntext.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Auther: jintianyun
 * @Date: 2020/10/28 11:18
 * @Description:
 */
public class dateUtil {
    /**
     *
     * 功能描述:解析16进制时间
     *
     * @param:
     * @return:
     * @auther: jintianyun
     * @date: 2020/10/27 13:48
     */
    public static Calendar getHexStringDate(String date) throws ParseException {
        int offset=0;
        Calendar cal1 = Calendar.getInstance();

        int YEAR=cal1.get(Calendar.YEAR);
        int MONTH=0;
        int DAY=0;
        int HOUR=0;
        int MINUTE=0;
        int SECOND=0;
        for(int i=0;i<date.length()/2;i++)
        {
            String o =date.substring(offset,offset+=2);
            int f =Integer.valueOf(o,16);
            switch (i){
                case 0:
                    break;
                case 1:
                    MONTH=f-1;
                    break;
                case 2:
                    DAY=f;
                    break;
                case 3:
                    HOUR=f;
                    break;
                case 4:
                    MINUTE=f;
                    break;
                case 5:SECOND=f;
            }
        }
        cal1.set(YEAR, MONTH, DAY, HOUR, MINUTE, SECOND);
        return cal1;
    }

    /**
     * 将未指定格式的日期字符串转化成java.util.Date类型日期对象
     *
     * @param date,待转换的日期字符串
     * @return
     * @throws ParseException
     */
    public static Date parseStringToDate(String date) throws ParseException{
        Date result=null;
        String parse=date;
        parse=parse.replaceFirst("^[0-9]{4}([^0-9]?)", "yyyy$1");
        parse=parse.replaceFirst("^[0-9]{2}([^0-9]?)", "yy$1");
        parse=parse.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)", "$1MM$2");
        parse=parse.replaceFirst("([^0-9]?)[0-9]{1,2}( ?)", "$1dd$2");
        parse=parse.replaceFirst("( )[0-9]{1,2}([^0-9]?)", "$1HH$2");
        parse=parse.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)", "$1mm$2");
        parse=parse.replaceFirst("([^0-9]?)[0-9]{1,2}([^0-9]?)", "$1ss$2");

        DateFormat format=new SimpleDateFormat(parse);

        result=format.parse(date);

        return result;
    }

}
